//
//  QuestionDetalisViewController.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/13.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "QuestionDetalisViewController.h"
#import "QuestionDetalisTableViewCell.h"
#import "Url_marco.h"
#import "GetDataHandle.h"
#import "QuestionDetalisModel.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>

#import <UMSocialData.h>
#import <UMSocialSnsService.h>

#import <UMSocialSnsPlatformManager.h>

@interface QuestionDetalisViewController ()<UITableViewDelegate,UITableViewDataSource,UMSocialUIDelegate>
@property (weak, nonatomic) IBOutlet UITableView *questionTableView;

@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
@property (weak, nonatomic) IBOutlet UIImageView *nameImage;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *introduceLabel;

@property (copy,nonatomic) NSMutableArray *dataArray;

@property (copy,nonatomic) NSMutableDictionary *dataDict;

@property (assign,nonatomic) CGFloat height;
@property (assign,nonatomic) CGFloat heightQuestion;

@property (strong,nonatomic) NSString *urlStr;

@end

@implementation QuestionDetalisViewController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableDictionary *)dataDict{
    if (!_dataDict) {
        _dataDict = [NSMutableDictionary dictionary];
    }
    return _dataDict;
}


- (void)viewWillAppear:(BOOL)animated{
//    隐藏导航条
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    代理
    self.questionTableView.delegate = self;
    self.questionTableView.dataSource = self;
    
//    注册cell
    [self.questionTableView registerNib:[UINib nibWithNibName:@"QuestionDetalisTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    
    
    //    下拉加载
    self.questionTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    
    //    上拉刷新
    
    self.questionTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
   [_questionTableView.mj_header beginRefreshing];
    
    
//    分享按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"分享"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAC:)];
    
//    取消tableview 中的换行线
    self.questionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    // Do any additional setup after loading the view from its nib.
}


- (void)rightAC:(UIBarButtonItem *)sender{
    NSString *str = [NSString stringWithFormat:@"%@",self.urlStr];
//    [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:_dictDataDicrionary[@"picurl"]];
        [UMSocialData defaultData].extConfig.title = @"分享的title";
    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = self.urlStr;
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"57b3dcf367e58e7ccf002006"
                                      shareText:str
                                     shareImage:[UIImage imageNamed:@"icon"]
                                shareToSnsNames:@[UMShareToSina,UMShareToTencent,UMShareToDouban,UMShareToQzone,UMShareToEmail,UMShareToSms,UMShareToQQ]
                                       delegate:self];

}


//下拉加载
- (void)headerRereshing{
//    将数组置空
    [self.dataArray removeAllObjects];
    [self getSession];
    [self.questionTableView.mj_header endRefreshing];
    self.questionTableView.tableHeaderView = nil;
    [self.questionTableView reloadData];
    
}

//上拉刷新
- (void)loadMoreData{
//    [self DropgetSession];
    [self.questionTableView.mj_footer endRefreshing];
    [self.questionTableView reloadData];
}


//数据解析
- (void)getSession{
    NSString *str = [NSString stringWithFormat:@"%@%@%@",QUESTIONDETALIS_URL,self.stringID,QUestPOST_URL];
    [[GetDataHandle sharedDataHandle]getDataWithUrlString:str withGetDataBlock:^(NSData *data) {
        NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *dic = di[@"data"];
        self.dataDict = dic[@"expert"];
        NSArray *array = dic[@"latestList"];
        
//        顶部数据
        [self.headerImage sd_setImageWithURL:[NSURL URLWithString:self.dataDict[@"picurl"]] placeholderImage:nil];
        [self.nameImage sd_setImageWithURL:[NSURL URLWithString:self.dataDict[@"headpicurl"]] placeholderImage:nil];
        self.nameLabel.text = self.dataDict[@"name"];
        self.titleLabel.text = self.dataDict[@"stitle"];
        self.introduceLabel.text = self.dataDict[@"description"];
        
        
        for (NSDictionary *dicti in array) {
            QuestionDetalisModel *model = [QuestionDetalisModel new];
            [model setValuesForKeysWithDictionary:dicti];
            [self.dataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.questionTableView.mj_header endRefreshing];
            [self.questionTableView reloadData];
        });
    }];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuestionDetalisTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    QuestionDetalisModel *model = self.dataArray[indexPath.row];
    cell.model  = model;
    
    NSDictionary *dic = model.answer;
    NSDictionary *dict = model.question;
    
   
    NSString *str = dic[@"content"];
 
    NSString *question = dict[@"content"];
    
    CGFloat height = [self textHeight:str];
    CGFloat heightQuestion = [self textHeight:question];
    cell.questionTitle.frame = CGRectMake(cell.questionTitle.frame.origin.x, cell.questionTitle.frame.origin.y, cell.questionTitle.frame.size.width, heightQuestion);
    cell.questionTitle.frame = CGRectMake(cell.questionTitle.frame.origin.x, cell.questionTitle.frame.origin.y, cell.questionTitle.frame.size.width, height);
    
    return cell;

    
}

//计数字符串所占矩形区域的大小，并返回高度
- (CGFloat)textHeight:(NSString *)test{
    //    计算字符串所占的大小
    CGRect rect = [test boundingRectWithSize:CGSizeMake(394, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return rect.size.height;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     QuestionDetalisModel *model = self.dataArray[indexPath.row];
    
    NSDictionary *dic = model.answer;
    NSDictionary *dict = model.question;
    
    
    NSString *str = dic[@"content"];
    
    NSString *question = dict[@"content"];
    
    CGFloat height = [self textHeight:str];
    CGFloat heightQuestion = [self textHeight:question];
    return height + heightQuestion + 170;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
